import { View, Image, Text } from '@tarojs/components'
import { useState, useEffect, useLayoutEffect, useCallback, useMemo } from 'react'
import Taro from '@tarojs/taro'
import { HttpGet, HttpDelete } from '@/network/servers'
import dayjs from 'dayjs'
import Utils from '@/utils/index'
import { connect } from 'react-redux'
import { getBrowserController, getNet, getStorage, getWxSDKParams } from '@/pages/components/utils';
import { LgPopLayer, LgEmpty } from 'lancoo-ui-mobile';
import '../../../images/public/style.css'
import './index.scss'
import { create } from 'lodash'
import Loading from '../../component/load'

const storage = getStorage()
var wx = require('weixin-js-sdk')
const {
  getQueryVariable,
  getDateGap,
} = Utils


const requireContext = require.context('../../../images/fileIcon', true, /^\.\/.*\.svg$/);
const svgs = requireContext.keys().map(requireContext);
const keys = requireContext.keys()
let fileObj = {}

keys.map((item, index) => {
  let str = item.replace('./', '').replace('.svg', '')
  fileObj[str] = svgs[index]
})


function App(props) {
  console.log(props, 'props');

  let {
    currentDate,
    scheduleList = [],
    year = 2025,
    userInfo,
    yearNum,
    selectMonth,
  } = props
  const [yearList, setYearList] = useState(scheduleList)

  // useEffect(() => {
  //   getScheduleYearList();
  // }, [year, yearNum])

  // 获取整年个人日程列表
  const getScheduleYearList = () => {
    HttpGet({
      url: `/api/schedule/list/person`,
      data: {
        userId: userInfo.userId,
        userType: userInfo.userType,
        eduId: userInfo.eduId,
        startTime: year + '-01-01',
        endTime: year + '-12-31',
      },
      load: true
    }).then((result) => {
      if (result.code === '200' && Array.isArray(result.data)) {
        let data = result.data;
        setYearList(data);
      }
    })
  }
  // 跳转发布页面
  const dayClick = (i, day,date) => {
    console.log(i, day, 'i, day');
    selectMonth(i, day,date)
  }
  const constructMonthTable = (year, month) => {
    let startWeek = new Date(year, month - 1, 1).getDay();
    let maxDay = new Date(year, month, 0).getDate();
    let arr: any = [];
    for (let i = 1; i <= maxDay; i++) {
      arr.push({
        day: i,
        dayText: i,
        date: year + "-" + month + "-" + i,
        list: []
      });
    }
    if (startWeek !== 1) {
      for (let i = 0; i < startWeek - 1; i++) {
        arr.unshift({});
      }
    }
    arr.forEach((item) => {
      scheduleList.forEach((child) => {
        if (item.date && dayjs(child.scheduleDate).format("YYYY-MM-DD") === dayjs(item.date).format("YYYY-MM-DD")) {
          item.list.push(child);
        }
      })
    })
    return arr;
  }
  const MonthArr = useMemo(() => {
    let arr: any = [];
    let curYear = dayjs().get('year');
    let curMonth = dayjs().get('month') + 1;
    let curDay = dayjs().get('date');
    for (let i = 1; i < 13; i++) {
      let dayArr = constructMonthTable(year, i);
      arr.push(
        <View className='year-month-one' key={i}>
          <View className='month-one-title' style={{ color: year === curYear && i === curMonth ? '' : '' }}>{i}月</View>
          <View className='month-one-day-list'>
            {
              dayArr.map((item, index) => (
                <View
                  onClick={() => {dayClick(i, item.day,item.date)}}
                  className={`month-one-day-one ${year === curYear && i === curMonth && item.day === curDay ? 'curDate' : ''} ${item.list?.length ? 'haveSchedule' : ''}`}
                  key={index}>
                  {
                    year === curYear && i === curMonth && item.day === curDay ?
                      <span className='curdate-text'>{item.day}</span> :
                      ''
                  }
                  {item.day}
                </View>
              ))
            }
          </View>
        </View>
      );
    }
    return arr;
  }, [year, yearList,scheduleList]);

  return (
    <View className='type-year-container'>
      {MonthArr}
    </View>
  )
}

const mainProps = ({ commonData }) => {
  return {
    userInfo: commonData.userInfo,
    env: commonData.env,
    noticeAddr: commonData.noticeAddr,
    baseUrl: commonData.baseUrl
  }
}

export default connect(mainProps)(App);
